//@ts-ignore
import dayjs from '@/common/dayjs.min.js'
//@ts-ignore
import localData from '@/common/dayjs.plugin.localData.js'
// config dayjs
dayjs.extend(localData)
dayjs.locale('zh-cn')

export const handleFormatDayjs = (date: any, exp = 'YYYY-MM-DD') => {
	return (date ? dayjs(date) : dayjs()).format(exp)
}

export const getLocalDayjs = () => {
	return dayjs
}

// 返回星期一为第一天的一周时间
export const weekDaysStartByMonday = (date: any) => {
	const today = dayjs(date)
	today.$locale().weekStart = 1
	const currentWeekStartDay = today.startOf('week')
	const currentWeekLastDay = today.endOf('week')
	const prevWeekStartDay = currentWeekStartDay.subtract(1, 'week')
	const nextWeekStartDay = currentWeekLastDay.add(1, 'week')
	return {
		currenWeekFirstDay: currentWeekStartDay,
		currenWeekLastDay: currentWeekLastDay,
		generateWeekDateList: [
			new Array(7)
				.fill('')
				.map((_, index) => prevWeekStartDay.add(index, 'day')),
			new Array(7)
				.fill('')
				.map((_, index) => currentWeekStartDay.add(index, 'day')),
			new Array(7)
				.fill('')
				.map((_, index) => nextWeekStartDay.add(index, 'day')),
		],
	}
}

/**
 * 返回是否重合
 * @param overDate 覆盖时间
 * @param areaDate 范围时间
 * @returns
 */
export const timeAreaOverlapCheck = (
	overDate: number[],
	areaDate: number[]
) => {
	const [s1, e1] = overDate
	const [s2, e2] = areaDate
	// 起始时间晚于范围时间 or  结束时间早于范围时间
	if (dayjs(s1).isAfter(dayjs(e2)) || dayjs(e1).isBefore(dayjs(s2))) {
		return false
	}

	return true
}
